//
//  LGFollowAddMedicineController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/27.
//  随访记录 -- 添加服药

#import "LGFollowAddMedicineController.h"
//搜索药品
#import "LGSearchMedicineController.h"
#import "LGAddMedicinLineView.h"
#import "LGPickerView.h"

#define saveTop  (Screen_H-bottomSafeBarHeight/2.0-viewPix(70))

@interface LGFollowAddMedicineController ()
/**   */
@property (nonatomic , strong)LGAddMedicinLineView *nameView;
/**   */
@property (nonatomic , strong)LGAddMedicinLineView *doseView;
/**   */
@property (nonatomic , strong)LGAddMedicinLineView *rateView;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)NSArray *doseArray;
/**   */
@property (nonatomic , strong)NSArray *rateArray;

/**   */
@property (nonatomic , copy)NSString *unitString;
/**   */
@property (nonatomic , copy)NSString *doseString;
/**   */
@property (nonatomic , copy)NSString *medicineId;

@end

@implementation LGFollowAddMedicineController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationView.title = @"添加服药";
    self.unitString = @"";
    [self.view addSubview:self.nameView];
    [self.view addSubview:self.doseView];
    [self.view addSubview:self.rateView];
    [self.view addSubview:self.saveBtn];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Followup/takeFrequency" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.doseArray = returnData[@"take_dose"];
            self.rateArray = returnData[@"take_frequency"];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.doseString = LGNSString(dataDic[@"advice_dose"]);
    self.medicineId = LGNSString(dataDic[@"medicine_id"]);
    self.unitString = LGNSString(dataDic[@"packing_unit"]);
    self.nameView.content = LGNSString(dataDic[@"medicine_name"]);
    self.doseView.content = [NSString stringWithFormat:@"%@ %@",dataDic[@"advice_dose"],dataDic[@"dose_unit"]];
    self.rateView.content = LGNSString(dataDic[@"advice_take_frequency"]);
    [self.view addSubview:self.deleteBtn];
    [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    self.saveBtn.frame = CGRectMake(kMaxX(self.deleteBtn.frame)+viewPix(24), saveTop, (Screen_W-viewPix(118))/2.0, viewPix(48));
}


-(void)setUnitString:(NSString *)unitString{
    _unitString = unitString;
    if(self.doseString.length>0){
        self.doseView.content = [NSString stringWithFormat:@"%@ %@",self.doseString,unitString];
    }
}


-(void)setDoseString:(NSString *)doseString{
    _doseString = doseString;
    self.doseView.content = [NSString stringWithFormat:@"%@ %@",doseString,self.unitString];
}

//药品名称
-(void)nameViewTouchAction{
    LGSearchMedicineController *controller = [[LGSearchMedicineController alloc]init];
    controller.canSelect = YES;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.selectMedicineResult = ^(NSDictionary * _Nonnull dataDic) {
        weakSelf.medicineId = LGNSString(dataDic[@"id"]);
        weakSelf.unitString = LGNSString(dataDic[@"packing_unit"]);
        weakSelf.nameView.content = [NSString stringWithFormat:@"%@(%@)",dataDic[@"title"],dataDic[@"dose"]];
    };
}

//单次剂量
-(void)doseViewTouchAction{
    if (self.doseArray.count > 0) {
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[self.doseArray];
        pickerView.selectStr = (self.doseString.length>0)?self.doseString:@"1";
        pickerView.units = @[self.unitString];
        [self.view.window addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            weakSelf.doseString = LGNSString(resultArray[0]);
        };
    }else{
        [self requestData];
    }
}


//服药频率
-(void)rateViewTouchAction{
    if (self.rateArray.count > 0) {
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[self.rateArray];
        pickerView.selectStr = (self.rateView.content.length>0)?self.rateView.content:LGNSString(self.rateArray[0]);
        [self.view.window addSubview:pickerView];
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickViewResult = ^(NSArray *resultArray) {
            weakSelf.rateView.content = LGNSString(resultArray[0]);
        };
    }else{
        [self requestData];
    }
    
}

//保存
-(void)saveBtnAction{
    if(self.medicineId.length == 0){
        [TooltipView showMessage:@"请选择药品名称" offset:0];
        return;
    }
    
    if(self.doseString.length == 0){
        [TooltipView showMessage:@"请设置单次剂量" offset:0];
        return;
    }
    
    if(self.rateView.content.length == 0){
        [TooltipView showMessage:@"请选择服药频率" offset:0];
        return;
    }
    
    NSDictionary *tempDic = @{@"followupMedicaladvice_id":@"0",@"advice_type":@"out",@"medicine_name":self.nameView.content,@"dose_unit":self.unitString,@"advice_dose":self.doseString,@"advice_take_frequency":self.rateView.content,@"reality_dose":@"",@"reality_take_frequency":@"",@"medicine_id":self.medicineId,@"stop_desc":@""};
    
    if(_dataDic && _dataDic.allKeys.count>0){
        if(self.editMedicineRecord){
            self.editMedicineRecord(tempDic, self.index);
        }
    }else{
        if(self.addMedicineRecord){
            self.addMedicineRecord(tempDic);
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)deleteButtonAction{
    if(self.deleteMedicineRecord){
        self.deleteMedicineRecord(self.index);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark ====> 创建控件
-(LGAddMedicinLineView *)nameView{
    if(!_nameView){
        _nameView = [[LGAddMedicinLineView alloc] initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(55))];
        _nameView.title = @"药品名称";
        [_nameView.actionBtn addTarget:self action:@selector(nameViewTouchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nameView;
}

-(LGAddMedicinLineView *)doseView{
    if(!_doseView){
        _doseView = [[LGAddMedicinLineView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.nameView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(55))];
        _doseView.title = @"单次剂量";
        [_doseView.actionBtn addTarget:self action:@selector(doseViewTouchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doseView;
}


-(LGAddMedicinLineView *)rateView{
    if(!_rateView){
        _rateView = [[LGAddMedicinLineView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.doseView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(55))];
        _rateView.title = @"服药频率";
        [_rateView.actionBtn addTarget:self action:@selector(rateViewTouchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rateView;
}

-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" topMargin:saveTop];
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithTitle:@"删除" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#DD3030" selectColor:@"#DD3030"];
        _deleteBtn.frame = CGRectMake(viewPix(47), saveTop, (Screen_W-viewPix(118))/2.0, viewPix(48));
        _deleteBtn.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        _deleteBtn.layer.borderWidth = 1;
        _deleteBtn.cornerRidus = 10;
        [_deleteBtn addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
